﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;

using Telerik.Windows.Controls;
using BrokerBase.Lib.Core;
using BrokerBase.Lib.Company;
using Broker.Model;

namespace Broker
{
    /// <summary>
    /// Interaction logic for LoginForm.xaml
    /// </summary>
    public partial class LoginForm
    {
        private bool isSuccess;
        private IList<ICompany> compies;
        private string defaultUserName;
        public LoginForm()
        {
            InitializeComponent();
            
            Loaded += new RoutedEventHandler(LoginForm_Loaded);
        }

        public LoginForm(string userName):this()
        {
            defaultUserName = userName;
            txtName.Text = defaultUserName;
        }

        void LoginForm_Loaded(object sender, RoutedEventArgs e)
        {
            LoadCompanies();
            cmbCompany.SelectedItem = compies.FirstOrDefault(p => p.Name == AppGlobal.ClientCfg.DefaultCompany);

            if (!string.IsNullOrEmpty(defaultUserName))
            {
                txtPassword.Focus();
            }
        }

        public bool IsSuccess { get { return isSuccess; } }


        private void btnLogin_Click(object sender, RoutedEventArgs e)
        {
            LoginVerify();
        }

        private void btnExit_Click(object sender, RoutedEventArgs e)
        {
            isSuccess = false;
            Close();
        }

        private void LoadCompanies()
        {


            IBaseService<Company> svr = new BaseService<Company>(AppGlobal.HbmSessionFactory.GetCurrentSession());

            compies = svr.GetListByHQL("from Company").Cast<ICompany>().ToList<ICompany>();

            ICompany cmp = new Company();
            cmp.Name = "[全部]";
            cmp.Id = 0;

            compies.Insert(0, cmp);

            cmbCompany.ItemsSource = compies;
        }

        private void btnCancel_Click(object sender, RoutedEventArgs e)
        {
            isSuccess = false;

            Close();
        }

        private void LoginVerify()
        {
            try
            {
                SysUser user = SysUser.LoadUser(AppGlobal.HbmSessionFactory.GetCurrentSession(), txtName.Text.Trim(), txtPassword.Password.Trim());

                if (user != null)
                {
                    AppGlobal.CurrentUser = user;

                    isSuccess = true;
                    this.Close();
                }
                else
                {
                    MessageBox.Show("用户名或密码错误!");
                }
            }
            catch (Exception ex)
            {
                AppGlobal.Log.Error("登录时发生错误", ex);
                MessageBox.Show("登录时发生错误", ex.Message);
            }
        }

        private void txtPassword_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.Key == Key.Enter)
            {
                LoginVerify();
            }
        }

        private void txtName_KeyDown(object sender, KeyEventArgs e)
        {
            if(e.Key==Key.Enter)
                txtPassword.Focus();
        }

        private bool VerifyName(string name)
        {
            return true;
        }
    }
}
